<?php
// +----------------------------------------------------------------------
// | 车钉钉 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\admin\controller;

/**
 * 产品控制器
 */
class Product extends Base{
    protected $brandList;//品牌
    /**
     * 前置操作方法列表
     * @var array $beforeActionList
     * @access protected
     */
    protected $beforeActionList = ['baseInfo'];
    /**
     * 构造方法，初始化站点信息
     */
    protected function _initialize(){
        parent::_initialize();
    }
    //模版信息初始化
    public function baseInfo(){
        $baseWebmsg=cache('baseWebMsg');
        $this->assign('baseWebMsg',$baseWebmsg);
        $brandList=db('car_brand')->select();
        $this->brandList=$brandList;
        $this->assign('brandList',$brandList);
    }
    
	//产品列表
    public function index(){
        $carData=db('v_car')->select();
        $this->assign('carData',$carData);
        return $this->fetch();
    }
    //添加产品页面
    public function productAdd(){
        $carYear=db('car_year')->select();
        $youhui=db('car_youhui')->select();
        $carType=db('car_type')->select();
        $this->assign('carType',$carType);
        $this->assign('carYear',$carYear);
        $this->assign('youhui',$youhui);
        return $this->fetch();
    }
    //ajax获取车系
    public function getProductXi(){
        $id=input('param.id/d');//品牌id
        $brandXi=db('car_brand')->where('pid',$id)->select();
        return $brandXi;
    }
    //提交
    public function productSave(){
        $ProductObj = model('Product');
        if(request()->isPost()){ //提交表单
            $data=request()->post();
            $data['name']=$data['typename']." ".$data['car_year']."款 ".$data['name'];
            //exit(json_encode($data));
            $insertId=$ProductObj->insert($data);
            if($insertId>=1){
                //保存图片路径 
                $imgsArr=explode(',', $data['imgs']);
                $FileUtil=new \FileUtil();
                foreach ($imgsArr as $v){
                    $fileName=substr($v,9);
                    $newPath= './static/img/product/'.$insertId."/".$fileName;
                    $p_newPath='./static/img/product/'.$insertId."/P_".$fileName;
                    $m_newPath='./static/img/product/'.$insertId."/M_".$fileName;
                    $FileUtil->moveFile(config('picture_upload.rootPath').$v, './static/img/product/'.$insertId."/".$fileName);
                    $image = \think\Image::open($newPath);
                    $image->thumb(config('picture_image.width'),config('picture_image.height'),\think\Image::THUMB_CENTER)->save($p_newPath);//生成缩略图
                    $image->thumb(config('picture_image_m.width'),config('picture_image_m.height'),\think\Image::THUMB_CENTER)->save($m_newPath); 
                    $newImgsArr[]=['car_id'=>$insertId,'img'=>$fileName];
                }
                $isOK1=db('car_img')->insertAll($newImgsArr);
                if($isOK1){
                    $isOK2=db('car_content')->insert(['car_id'=>$insertId,'content'=>$data['editorValue']]);
                    if($isOK2){
                        return ['code'=>200,'msg'=>'提交成功'];
                    }else{
                        return ['code'=>300,'msg'=>'车辆亮点保存失败'];
                    }
                }else{
                    return ['code'=>300,'msg'=>'图片保存失败'];
                }
            }else{
                return ['code'=>300,'msg'=>$ProductObj->getError()];
            }
        }else {
            return ['code'=>400,'msg'=>'非法请求'];
        }
    }
    //ajax删除
    public function delete(){
        $id=input('param.id/d');//品牌id
        $isOK=db('car')->delete($id);
        if($isOK){
            $FileUtil=new \FileUtil();
            $isDel=$FileUtil->unlinkDir('./static/img/product/'.$id);
            if($isDel){
                return ['code'=>200,'msg'=>'删除成功'];
            }else {
                return ['code'=>300,'msg'=>'文件删除失败'];
            }
        }else{
            return ['code'=>400,'msg'=>'删除失败'];
        }
    }
    //ajax修改显示
    public function isDisplay(){
        $id=input('param.id/d');//品牌id
        $isDisplay=db('car')->where('id',$id)->find();
        if($isDisplay['isdisplay']==1){
            $isOK=db('car')->where('id',$id)->update(['isdisplay'=>0]);
        }else{
            $isOK=db('car')->where('id',$id)->update(['isdisplay'=>1]);
        }
        if($isOK){
            return ['code'=>200,'msg'=>'修改成功'];
        }else{
            return ['code'=>400,'msg'=>'修改失败'];
        }
    }
}
